package com.echo.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.Image;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.echo.activity.R;
import com.echo.http.HttpCallBack;
import com.echo.model.User;
import com.echo.util.DrawableUtil;
import com.echo.view.RoundedImageView;
import com.github.mikephil.charting.charts.LineChart;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by EchoGe on 2016/6/15.
 */
public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder>{

    private Context mContext;
    private List<User> mUsers=new ArrayList<User>();
    private ImageLoader mImageLoader;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private RoundedImageView head_image;
        private TextView nickname;

        public ViewHolder(View itemView) {
            super(itemView);
            head_image= (RoundedImageView) itemView.findViewById(R.id.item_friend_head_image);
            nickname= (TextView) itemView.findViewById(R.id.item_friend_nickname);
        }
    }

    public FriendAdapter(){}

    public FriendAdapter(Context context, List<User> users){
        this.mContext=context;
        this.mUsers=users;
        mImageLoader= ImageLoader.getInstance();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_friend,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        view.setTag(viewHolder);
        return viewHolder;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.nickname.setText(mUsers.get(position).getNickname());
        if(mUsers.get(position).getHeadImage()!=null&&mUsers.get(position).getHeadImage().length>0){
            holder.head_image.setImageBitmap(BitmapFactory.decodeByteArray(mUsers.get(position).getHeadImage(),0,mUsers.get(position).getHeadImage().length));
        }else{
            if(!mUsers.get(position).getHeadImageUrl().equals("")) {
                mImageLoader= ImageLoader.getInstance();
                mImageLoader.init(ImageLoaderConfiguration.createDefault(mContext));
                mImageLoader.displayImage(mUsers.get(position).getHeadImageUrl(), holder.head_image);
            }else{
                holder.head_image.setBackgroundColor(Color.parseColor("#84be1e"));
            }
        }
    }

    @Override
    public int getItemCount() {
        return mUsers.size();
    }

}
